﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using MammothMicro.SMTPGuard.Common;
using MammothMicro.SmtpGuardFilter.Filter;

namespace MammothMicro.SMTPGuard.Filter
{
    /// <summary>
    /// Handles mail filtering 
    /// </summary>
    public class FilterManager 
    {
        private readonly TimeSpan _refreshTimeSpan;
        private static Timer _timer;

        ///<summary>
        /// Stores filtered messages
        ///</summary>
        public static IMailStore MailStore { get;set;}
        /// <summary>
        /// Null sender filter  on/off
        /// </summary>
        public static bool BlockNullSender { get; set; }
        /// <summary>
        /// Null subject filter on/off
        /// </summary>
        public static bool BlockNullSubject { get; set;}
        /// <summary>
        /// Use spam assasin to filter
        /// </summary>
        public static bool UseSpamAssasin { get; set; }
        /// <summary>
        /// Use ClamWin anti-virus
        /// </summary>
        public static bool UseClamWin { get; set; }
  
        ///<summary>
        /// Filter attachments
        ///</summary>
        public static AttachmentFilter AttachmentFilter = new AttachmentFilter();
        /// <summary>
        /// Filter against blocked email list
        /// </summary>
        public static BlockedEmailFilter BlockedEmailFilter = new BlockedEmailFilter();
        /// <summary>
        /// Filter against blocked Ip list
        /// </summary>
        public static BlockedIpListFilter BlockedIpFilter = new BlockedIpListFilter();
        /// <summary>
        /// Filter against blocked keywords list
        /// </summary>
        public static BlockedKeywordFilter BlockedKeywordFilter = new BlockedKeywordFilter();
        /// <summary>
        /// Domain white list
        /// </summary>
        public static DomainWhiteListFilter DomainWhiteListFilter = new DomainWhiteListFilter();
        /// <summary>
        /// Email white list 
        /// </summary>
        public static EmailWhiteListFilter EmailWhiteListFilter = new EmailWhiteListFilter();
        /// <summary>
        /// Local recipient list - user must be on this list to get email's
        /// </summary>
        public static LocalRecipientFilter LocalRecipientFilter = new LocalRecipientFilter();
        /// <summary>
        /// Filters null senders
        /// </summary>
        public static NullSenderFilter NullSenderFilter = new NullSenderFilter();
        /// <summary>
        /// Filters null subject
        /// </summary>
        public static NullSubjectFilter NullSubjectFilter = new NullSubjectFilter();
        /// <summary>
        /// Filters by rules in the list
        /// </summary>
        public static RulesFilter RulesFilter = new RulesFilter();
        /// <summary>
        /// Checks email against trusted Ip list
        /// </summary>
        public static TrustedIpFilter TrustedIpFilter = new TrustedIpFilter();
        /// <summary>
        /// Runs anti-virus scan by ClamWin
        /// </summary>
        public static ClamWinFilter ClamWinFilter = new ClamWinFilter();
        /// <summary>
        /// Filters by SpamAssasin
        /// </summary>
        public static SpamAssasinFilter SpamAssasinFilter = new SpamAssasinFilter();
        /// <summary>
        /// Filters by remote RBL list
        /// </summary>
        public static RblFilter RblFilter = new RblFilter();


        ///<summary>
        ///</summary>
        ///<param name="refreshTimeSpan">Timer to reload filter lists</param>
        ///<param name="mailStore">Store filtered emails</param>
        public FilterManager(TimeSpan refreshTimeSpan, IMailStore mailStore)
        {
            LoadCache();
            MailStore = mailStore;
            _refreshTimeSpan = refreshTimeSpan;
            _timer = new Timer(_refreshTimeSpan.TotalMilliseconds);
            _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            _timer.Start();

        }

        static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
           // Reload filter cache
           LoadCache();
        }
        
        private static void LoadCache()
        {
            AttachmentFilter.Reload();
            BlockedEmailFilter.Reload();
            BlockedIpFilter.Reload();
            BlockedKeywordFilter.Reload();
            DomainWhiteListFilter.Reload();
            EmailWhiteListFilter.Reload();
            LocalRecipientFilter.Reload();
            RulesFilter.Reload();
            TrustedIpFilter.Reload();
            // ClamWinFilter.Reload();
            // SpamAssasinFilter.Reload();
            // RblFilter.Reload();
            
        }
       
    
    }
}
